/*
 * HistoryCursorAdapter.java
 *
 * Copyright(c) 2010
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic.history;

import de.studiorutton.colorschemes.IColorSchemeMapper;
import de.studiorutton.gerengdic.Prefs;
import de.studiorutton.gerengdic.R;
import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class HistoryCursorAdapter extends CursorAdapter {
	private Cursor cursor;

	public HistoryCursorAdapter(Context context, Cursor c) {
		super(context, c);
		cursor = c;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		IColorSchemeMapper csmi = Prefs.getInstance()
				.getColorSchemeMapper();
		
		RelativeLayout rl = (RelativeLayout) view.findViewById( R.id.historyrow );
		TextView tvSearchString = (TextView) view
				.findViewById(R.id.search_string);
		TextView tvSearchWay = (TextView) view.findViewById(R.id.search_way);
		ImageView imArrow = (ImageView) view.findViewById(R.id.arrow);

		String searchString = cursor.getString(cursor
				.getColumnIndex(HistoryProvider.SEARCH_STRING));
		int searchWay = cursor.getInt(cursor
				.getColumnIndex(HistoryProvider.SEARCH_WAY));

		int position = cursor.getPosition();
		if (position % 2 == 0) {
			tvSearchString.setTextColor(csmi.getResultContentTextcolorOne());
			tvSearchWay.setTextColor(csmi.getResultContentTextcolorOne());
			imArrow.setImageDrawable(csmi.getResultContentArrowOne());
			rl.setBackgroundDrawable(csmi
					.getResultContentBackgroundOne());
		} else {
			tvSearchString.setTextColor(csmi.getResultContentTextcolorTwo());
			tvSearchWay.setTextColor(csmi.getResultContentTextcolorTwo());
			imArrow.setImageDrawable(csmi.getResultContentArrowTwo());
			rl.setBackgroundDrawable(csmi
					.getResultContentBackgroundTwo());
		}

		tvSearchString.setText(searchString);
		if (searchWay == AbstractCommand.SEARCH_WAY_FULLTEXT)
			tvSearchWay.setText(R.string.fulltext_search);
		else
			tvSearchWay.setText(R.string.indexed_search);
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		LayoutInflater li = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		return li.inflate(R.layout.historylistitemrow, parent, false);
	}

}
